package com.cloudkinto.controller.finance;


import com.cloudkinto.base.BaseController;
import com.cloudkinto.common.annotations.UserSessionRequired;
import com.cloudkinto.common.web.PageResult;
import com.cloudkinto.common.web.SingleResult;
import com.cloudkinto.service.finance.CostRecordService;
import com.cloudkinto.service.finance.vo.CostRecordListRes;
import io.swagger.annotations.*;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author ding
 * @since 2020-06-10
 */
@RestController
@Api(tags = "财务-支出记录模块")
@RequestMapping("/api/admin/cost")
public class CostRecordController extends BaseController {
    private final Logger logger = LoggerFactory.getLogger(CostRecordController.class);

    @Autowired
    private CostRecordService service;

    @UserSessionRequired
    @GetMapping("/addInit")
    @ApiOperation(value = "新增初始化", notes = "新增初始化")
    public SingleResult addInit() {
        return service.addInit();
    }

    @PostMapping("")
    @ApiOperation(value = "新增", notes = "新增")
    public SingleResult add(@RequestBody @Validated Object req) {
        Long userId = getCurrentUserId();
        Long companyId = getCompanyId();
        return SingleResult.success("新增成功", service.add(req, userId));
    }

    @UserSessionRequired
    @GetMapping("/updateInit/{id}")
    @ApiOperation(value = "编辑初始化", notes = "编辑初始化")
    public SingleResult updateInit(@PathVariable("id") Long id) {
        return service.updateInit(id);
    }

    @UserSessionRequired
    @PutMapping("")
    @ApiOperation(value = "修改", notes = "修改")
    public SingleResult update(@RequestBody @Validated Object req) {
        Long userId = getCurrentUserId();
        return SingleResult.success("修改成功", service.update(req, userId));
    }

    @UserSessionRequired
    @DeleteMapping("/{id}")
    @ApiOperation(value = "根据id删除", notes = "根据id删除")
    public SingleResult delete(@PathVariable("id") Long id) {
        Long userId = getCurrentUserId();
        return SingleResult.success("删除成功", service.delete(id, userId));
    }

    @UserSessionRequired
    @GetMapping("/{id}")
    @ApiOperation(value = "根据id查看详情", notes = "根据id查看详情")
    public SingleResult detail(@PathVariable("id") Long id) {
        return SingleResult.success("查询成功", service.detail(id));
    }

    @UserSessionRequired
    @GetMapping("/pageInit")
    @ApiOperation(value = "查询初始化", notes = "查询初始化")
    public SingleResult pageInit() {
        return service.pageInit();
    }

    @UserSessionRequired
    @GetMapping("/list")
    @ApiOperation(value = "查询", notes = "查询")
    @ApiImplicitParams(@ApiImplicitParam(value = "关键字(模糊查询)" ,name = "keyword",paramType = "query",dataType = "String"))
    public SingleResult list(HttpServletRequest request) {
        Long companyId = getCompanyId();

        Map<String, String> map = new HashMap<>();
        String keyword = request.getParameter("keyword");
        map.put("keyword", keyword);
        return SingleResult.success("查询成功", service.getList(map));
    }

    @UserSessionRequired
    @GetMapping("/listPage")
    @ApiOperation(value = "查询（分页）", notes = "查询（分页）")
    @ApiImplicitParams({@ApiImplicitParam(name = "dateStart", value = "开始时间"), @ApiImplicitParam(name = "dateEnd", value = "结束时间")
            , @ApiImplicitParam(name = "type", value = "pageInit 返回数据")})
    @ApiResponses({@ApiResponse(code = 200, message = "", response = CostRecordListRes.class)})
    public PageResult listPage(String dateType,String dateStart, String dateEnd, String type, String current, String pageSize, Long companyId, String keyword) {

        Map<String, String> map = new HashMap<>();
        map.put("dateStart", dateStart);
        map.put("dateEnd", dateEnd);
        map.put("current", current);
        map.put("pageSize", pageSize);
        map.put("type", type);
        map.put("keyword", keyword);
        if (companyId == null) {
            companyId = getCompanyId();
        }
        if (companyId != null) {
            map.put("companyId", companyId + "");
        }
        if (StringUtils.isBlank(dateType)) {
            map.put("dateType","createTime");
        }
        else {
            map.put("dateType", dateType);
        }
        return service.getListPage(map);
    }

    @UserSessionRequired
    @GetMapping("exportExcel")
    @ApiOperation(value = "导出所有数据excel")
    @ApiImplicitParams({@ApiImplicitParam(name = "dateStart", value = "开始时间"), @ApiImplicitParam(name = "dateEnd", value = "结束时间")
            , @ApiImplicitParam(name = "type", value = "pageInit 返回数据")})
    public void exportAll(String dateType,String dateStart, String dateEnd, String type, Long companyId, String current, String pageSize, HttpServletResponse response) throws Exception {
        Map<String, String> map = new HashMap<>();

        map.put("dateStart", dateStart);
        map.put("dateEnd", dateEnd);
        map.put("current", current);
        map.put("pageSize", pageSize);
        map.put("type", type);
        if (companyId == null) {
            companyId = getCompanyId();
        }
        if (companyId != null) {
            map.put("companyId", companyId + "");
        }
        if (StringUtils.isBlank(dateType)) {
            map.put("dateType","createTime");
        }
        else {
            map.put("dateType", dateType);
        }
        service.exportExcel(map, response);
    }

}

